package com.xuelang.pipeline.artifact.app.resource.impl;

import cn.hutool.core.util.IdUtil;
import com.xuelang.pipeline.artifact.app.resource.ArtifactNamespaceService;
import com.xuelang.pipeline.artifact.app.resource.converter.ArtifactNamespaceDTOToArtifactNamespaceConverter;
import com.xuelang.pipeline.artifact.client.common.request.PageParam;
import com.xuelang.pipeline.artifact.client.common.result.PageRecord;
import com.xuelang.pipeline.artifact.client.resource.dto.ArtifactNamespaceDTO;
import com.xuelang.pipeline.artifact.client.resource.dto.ArtifactNamespaceQuery;
import com.xuelang.pipeline.artifact.domain.resource.ArtifactNamespace;
import com.xuelang.pipeline.artifact.domain.resource.service.ArtifactNamespaceDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 镜像仓库命名空间服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class ArtifactNamespaceServiceImpl implements ArtifactNamespaceService {

    @Autowired
    ArtifactNamespaceDomainService artifactNamespaceDomainService;

    @Override
    public PageRecord<ArtifactNamespaceDTO> pageList(PageParam<ArtifactNamespaceQuery> dtoPageParam) {
        ArtifactNamespaceQuery condition = dtoPageParam.getCondition();
        condition = condition == null ? new ArtifactNamespaceQuery() : condition;
        PageRecord<ArtifactNamespace> pageRecord = artifactNamespaceDomainService.pageList(
                dtoPageParam.getCurrentPage(),
                dtoPageParam.getPageSize(),
                condition);
        return new PageRecord<>(
                pageRecord.getCurrentPage(),
                pageRecord.getPageSize(),
                pageRecord.getTotalCount(),
                pageRecord.getTotalPages(),
                pageRecord.getRecords().stream().map(ArtifactNamespaceDTOToArtifactNamespaceConverter.INSTANCE::artifactNamespaceToArtifactNamespaceDTO).collect(Collectors.toList()));
    }

    @Override
    public List<ArtifactNamespaceDTO> list(ArtifactNamespaceQuery artifactNamespaceQuery) {
        return artifactNamespaceDomainService.list(artifactNamespaceQuery).stream().map(ArtifactNamespaceDTOToArtifactNamespaceConverter.INSTANCE::artifactNamespaceToArtifactNamespaceDTO).collect(Collectors.toList());
    }


    @Override
    public Boolean add(ArtifactNamespaceDTO artifactNamespaceDTO) {
        ArtifactNamespace artifactNamespace = ArtifactNamespaceDTOToArtifactNamespaceConverter.INSTANCE.artifactNamespaceDTOToArtifactNamespace(artifactNamespaceDTO);
        artifactNamespace.setUuid(IdUtil.fastSimpleUUID());
        return artifactNamespaceDomainService.save(artifactNamespace);
    }

    @Override
    public Boolean update(ArtifactNamespaceDTO artifactNamespaceDTO) {

        ArtifactNamespace artifactNamespace = ArtifactNamespaceDTOToArtifactNamespaceConverter.INSTANCE.artifactNamespaceDTOToArtifactNamespace(artifactNamespaceDTO);
        return artifactNamespaceDomainService.update(artifactNamespace);
    }

    @Override
    public Boolean delete(String uuid) {
        return artifactNamespaceDomainService.delete(uuid);
    }

    @Override
    public ArtifactNamespaceDTO query(String uuid) {
        return ArtifactNamespaceDTOToArtifactNamespaceConverter.INSTANCE.artifactNamespaceToArtifactNamespaceDTO(artifactNamespaceDomainService.query(uuid));
    }

}
